import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/Helper.dart';
import '../../helper/global_config.dart';
import '../../pages/things/things_helper.dart';
import '../../widgets/inputWidget.dart';
import '../../model/things.dart';
import '../../helper/routeHelper.dart';
import '../../helper/uidata.dart';
import '../../model/skddl.dart';
import '../skddl/sdddl_authorty.dart';
import '../../widgets/someCards.dart';

class ThingsSelectPage extends StatefulWidget {
  final bool ifAppbar;
  final ThingsSelectCallback callback;
  @override
  ThingsSelectPage(this.ifAppbar, this.callback);
  @override
  ThingsSelectPageState createState() => new ThingsSelectPageState();
}

class ThingsSelectPageState extends State<ThingsSelectPage> {
  List<Things> RList = <Things>[];
  List<Things> RList2 = <Things>[];

  bool isEnd = false;
  ScrollController _scrollController = new ScrollController();
  bool firstLoading = false;
  int canShow = 0;

  late Things X1;
  late String sData;

  final TextEditingController c1 = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    getRefrush();
  }

  Future<Null> delete(Things r1) async {
    // await addOne();
    await Things.Delete(r1);
    setState(() {
      RList.remove(r1);
    });
  }

  onDeleted(Things r1) => {
        setState(() {
          RList.remove(r1);
        })
      };

  Future<Null> getRefrush() async {
    var list1 = await Things.GetAllList();
    setState(() {
      RList = list1;
      RList2 = list1;
      c1.text = "";
    });
    // search();
  }

  Widget thingsCard(Things x1) {
    return ThingsHelper.thingsCard(
      x1,
      () {
        print(x1.TName);
        widget.callback(x1);
        Navigator.of(context).pop();
      },
    );
  }

  void search() {
    var list1 = <Things>[];
    sData = c1.text;
    for (var item in RList) {
      if (item.TName.contains(sData) ||
          item.TType.contains(sData) ||
          item.Storage.contains(sData)) {
        list1.add(item);
      }
    }
    RList2 = list1;
    this.setState(() {});
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list5 = <Widget>[];

    list3.add(InputWidget.getSearchButton(c1, search));

    list1.add(InputWidget.inCard2(list3));
    list1.add(listContainer());
    list1.add(InputWidget.inCard2(list4));

    return list1;
  }

  Widget listContainer() {
    return Expanded(
        child: Container(
            // height: 500.0,
            margin: EdgeInsets.only(top: 8.0),
            child: new RefreshIndicator(
              child: ListView.builder(
                itemCount: RList2.length + 1,
                controller: _scrollController,
                physics: const AlwaysScrollableScrollPhysics(),
                itemBuilder: (BuildContext context, int index) {
                  if (index != RList2.length) {
                    return thingsCard(RList2[index]);
                  } else {
                    if (isEnd == true) {
                      return Container(
                          margin: EdgeInsets.all(12.0),
                          child: Center(
                              child: Text(
                            "没有更多了",
                            style: TextStyle(color: Colors.grey),
                          )));
                    } else {
                      return new Container();
                    }
                  }
                },
              ),
              onRefresh: getRefrush,
            )));
  }

  @override
  Widget build(BuildContext context) {
    if (RList == null) return new Container();
    // TODO: implement build
    return new Scaffold(
        appBar: widget.ifAppbar
            ? AppBar(
                title: Text('选择物资'),
                actions: <Widget>[
                  IconButton(
                      icon: Icon(
                        Icons.add,
                        color: Colors.white,
                      ),
                      onPressed: () {
                        RouteHelper.ThingsAdd(context, null, getRefrush);
                      }),
                ],
              )
            : null,
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.refresh),
          onPressed: () => getRefrush(),
        ),
        body: firstLoading == true
            ? Center(
                child: SpinKitCircle(
                color: Colors.blue,
              ))
            : Column(children: getWidgetList(context)));
  }
}
